package com.desire.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

/**
 * @author LittleDesire
 * @description
 * @date 2022-04-22 周五 10:45
 * @since 17
 */
@Controller
public class IndexController {
    @RequestMapping()
    public String index(Model m) {
        m.addAttribute("msg", "index首页");
        return "index";
    }


    @RequestMapping("fileUpload")
    public String fileUpload(String username, MultipartFile headFile, HttpServletRequest req) throws IOException {
        System.out.println(username+ headFile.getOriginalFilename());
        String path = req.getServletContext().getRealPath("uploads/");
        File f = new File(path);
        if (f.exists()) {
            f.mkdirs();
        }
        System.out.println(path);
        headFile.transferTo(new File(path, Objects.requireNonNull(headFile.getOriginalFilename())));
        return "index";
    }

    @RequestMapping("down")
    public void fileDownload(HttpServletRequest req, HttpServletResponse resp) {
        File file = new File("C:\\Users\\desire\\Desktop\\ssm\\boge\\_2springmvc\\_03updownfile\\target\\_03updownfile-1.0\\uploads\\","1.png");
        // 设置响应头信息
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("multipart/form-data");
        resp.setHeader("Content-Disposition", "attachment;fileName=" + file.getName());

        try {
            // 打开需要下载的文件
            FileInputStream is = new FileInputStream(file);
            // 激活下载的文件
            ServletOutputStream os = resp.getOutputStream();
            // 实现下载
            byte[] bytes = new byte[1024 * 1024];
            int length = 0;
            while ((length = is.read(bytes)) > 0) {
                os.write(bytes,0,length);
            }
            os.close();
            is.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("d")
    public ResponseEntity<byte[]> d(HttpServletRequest req) throws IOException {
        File file = new File("C:\\Users\\desire\\Desktop\\ssm\\boge\\_2springmvc\\_03updownfile\\target\\_03updownfile-1.0\\uploads\\","1.png");
        byte[] body = null;
        FileInputStream in = new FileInputStream(file);
        body = new byte[in.available()];
        in.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;fileName=" + file.getName());
        HttpStatus status =   HttpStatus.OK;
        return new ResponseEntity<>(body, headers, status);
    }

}
